package info.openlvb.openface.client.handler;

import info.openlvb.openface.client.base.Group;
import info.openlvb.openface.client.base.person.AddGroupPanel;
import info.openlvb.openface.client.service.LdapService;
import info.openlvb.openface.client.service.LdapServiceAsync;
import info.openlvb.openface.client.service.SambaService;
import info.openlvb.openface.client.service.SambaServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.HTML;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;

public class AddGroupListener extends ButtonListenerAdapter{

	private final SambaServiceAsync sambaService = GWT.create(SambaService.class);
	private final LdapServiceAsync ldapService = GWT.create(LdapService.class);
	private AddGroupPanel agp;
	public AddGroupListener(AddGroupPanel agp){
		this.agp = agp;
	}

	public void onClick(Button button, EventObject e) {
		ldapService.searchGroupByCn(agp.getNom().getText(), 
				new AsyncCallback<Group>(){
			public void onFailure(Throwable caught) {
				MessageBox.alert("probleme ...", "probleme de communication avec le backend");
			}
			public void onSuccess(Group result) {
				if(result!=null){
					MessageBox.alert("groupe existant", "Le groupe que vous avez voulu ajouter existe déjà !!");
					agp.getNom().setValue("");
					agp.getDescription().setValue("");
					agp.setNullMembers();
				}else{

					sambaService.addGroupShare(agp.getNom().getText(), 
							new AsyncCallback<Void>() {
						public void onFailure(Throwable caught) {
							final DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(true);
							simplePopup.setWidget(new HTML("probleme ajout partage de groupe"));
							simplePopup.center();
							simplePopup.show();
						}
						public void onSuccess(Void result) {
							final DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(true);
							simplePopup.setWidget(new HTML("Le partage de groupe a été créé"));
							simplePopup.center();
							simplePopup.show();
						}
					});
					Group group = new Group();
					group.setCn(agp.getNom().getText());
					group.setDescription(agp.getDescription().getText());
					group.setDisplayname(agp.getNom().getText());
					group.setMembers(agp.getMembers());

					ldapService.addGroup(group, 
							new AsyncCallback<Void>(){
						public void onFailure(Throwable caught) {
							final DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(true);
							simplePopup.setWidget(new HTML("probleme ajout de groupe dans le LDAP"));
							simplePopup.center();
							simplePopup.show();
						}
						public void onSuccess(Void result) {
							final DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(true);
							simplePopup.setWidget(new HTML("Le groupe a été créé"));
							simplePopup.center();
							simplePopup.show();
							agp.getNom().setValue("");
							agp.getDescription().setValue("");
							agp.setNullMembers();
						}
					});
					
				}}
		});
	}

}
